package com.hyy.book2.service;

import com.hyy.book2.constants.Constants;
import com.hyy.book2.mapper.BookMapper;
import com.hyy.book2.mapper.UserMapper;
import com.hyy.book2.model.BookInfo;
import com.hyy.book2.model.UserInfo;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Slf4j
@Service
public class UserService {


    @Autowired
    private UserMapper userMapper;




    public Boolean checkByNameAndPassword(String name, String password, HttpSession session) {
        log.info("开始对用户名及其密码进行逻辑判断");
        //没有输入账号或者密码返回假
        if (!StringUtils.hasText(name) || !StringUtils.hasText(password)) {
            log.info("没有输入账号或者密码");
            return false;
        }

        //判断之前是否登陆过
        if (session.getAttribute(Constants.SESSION_USER_YEY) != null) return true;


        //在服务器中查找是否有此用户
        UserInfo userInfo = userMapper.getUserInfoByName(name);
        //数据库没有这个用户返回假
        if (userInfo == null) {
            return false;
        }
        //有这个用户，把他的信息放到session中
        if (password.equals(userInfo.getPassword())) {
            session.setAttribute(Constants.SESSION_USER_YEY, userInfo.getUserName());
        }

        return true;
    }
}
